package com.cg.controller;

import com.cg.feign.OrderServer;
import com.cg.feign.StockServer;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/business")
@RequiredArgsConstructor
@Slf4j
public class BusinessController {

    private final StockServer stockServer;

    private final OrderServer orderServer;


    @GlobalTransactional
    @GetMapping("/purchase/{userId}/{stockId}")
    public void purchase(@PathVariable(value = "userId") Integer userId, @PathVariable(value = "stockId") Integer stockId) {
        log.info("执行......................");
        orderServer.generateOrder(userId, 1);
        stockServer.reduceStockNum(stockId, 10);
        log.info("执行完成...................");
    }

}
